package servlet;

import bean.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "CompeteServlet", value = "/athlete/CompeteServlet")
public class CompeteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        response.setContentType("text/html;charset=utf-8");
        String id=request.getParameter("id");
        String result=request.getParameter("result");
        String name=(String) session.getAttribute("userName");
        String comInfo = "";
        ApplyBean applyBean=new ApplyBean();
        applyBean.link();
        applyBean.loadApply();
        Apply app=null;
        int flag=0;

            for(Apply a:applyBean.getApplyArrayList()){
                if(id.equals(a.getId())&&name.equals(a.getName())){
                    app=a;
                    flag=1;
                }
            }
            if(flag==1){
                app.setResult(result);
                applyBean.updateApply(app);
                comInfo="比赛结束";
                session.setAttribute("comInfo",comInfo);
                response.sendRedirect("complete.jsp");
            } else{
                comInfo="您没有报名该项目！";
            session.setAttribute("comInfo",comInfo);
            response.sendRedirect("complete.jsp");
        }






    }
}